package spring_05_aop.example.cglib;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
/** 
 * 使用cglib动态代理 
 *  
 * @author student 
 *  
 */  
public class BookFacadeCglib implements MethodInterceptor {
	
	private Object object;
	
	/** 
     * 创建代理对象 
     *  
     * @param target 
     * @return 
     */  
	public Object getInstance(Object target){
		this.object = target;
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(this.object.getClass());
		// 回调方法  
		enhancer.setCallback(this);
		// 创建代理对象  
		return enhancer.create();
	}
	
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("事物开始");  
		proxy.invokeSuper(obj, args);
		System.out.println("事物结束"); 
		return null;
	}

}
